iT邦幫忙

2024 iThome 鐵人賽

DAY 24
0

上一篇有教怎麼創建對話框,這篇就不再介紹,還有不懂怎麼創建對話框,可以去看上一篇。
這篇主要介紹,Dialog 常用4種對話框,以下是比較常會用到的4種對話框:

普通對話框(2個按鈕)
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("購物車");
        dialog.setMessage("商品");

        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        dialog.setPositiveButton("確認購買", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        dialog.show();
}

https://ithelp.ithome.com.tw/upload/images/20241002/20168805P6uFDM0Ihh.png

普通對話框(多個按鈕)
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("購物車");
        dialog.setMessage("商品");

        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        dialog.setPositiveButton("確認購買", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.setNeutralButton("+  or -", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               
            }
        });

        dialog.show();
    }

https://ithelp.ithome.com.tw/upload/images/20241002/20168805J2JkCjX8rm.png

列表型對話框
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] items = {"葡萄", "橘子", "蘋果", "酪梨"};
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("商品列表");

        dialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "你點擊了" + items[i], Toast.LENGTH_SHORT).show();
            }
        });

        dialog.show();
    }

https://ithelp.ithome.com.tw/upload/images/20241002/20168805ZrcU45xgCn.png

等待型對話框
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("進度");
        dialog.setMessage("等待中...");
        dialog.show();

    }
}

https://ithelp.ithome.com.tw/upload/images/20241002/20168805BqBMkczrtF.png

Dialog 應用4種對話框 介紹完畢

下一篇 簡單介紹Fragment


上一篇
# Day 23 簡單介紹 Dialog
下一篇
# Day25 簡單介紹 Fragment
系列文
當Java遇見Android,30天學習指南30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言